#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <cassert>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
#define DEV "/dev/null"
void daemonSelf(const char *currPath = nullptr)
{
    signal(SIGPIPE, SIG_IGN);

    if (fork() > 0)
        exit(0);
    pid_t id = setsid();
    assert(id != -1);
    int fd = open(DEV, O_RDWR);
    if (fd >= 0)
    {
        dup2(fd, 0);
        dup2(fd, 1);
        dup2(fd, 2);
    }
    else
    {
        close(0);
        close(1);
        close(2);
    }
    if (currPath)
        chdir(currPath);
}